<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="40%"
    @close="handleClose"
    top="50px"
  >
    <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
      <el-form-item label="车主姓名" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="车主电话" prop="phone">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="汽车品牌" prop="brandId">
        <el-select v-model="form.brandId" placeholder="请选择">
          <el-option v-for="brandDatas in brandData" :key="brandDatas.id" :value="brandDatas.id" :label="brandDatas.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="汽车颜色" prop="colorId">
        <el-select v-model="form.colorId" clearable placeholder="请选择">
          <el-option v-for="colorDatas in colorData" :key="colorDatas.id" :value="colorDatas.id" :label="colorDatas.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="汽车车牌" prop="plateId">
        <el-select v-model="form.plateId" clearable placeholder="请选择">
          <el-option
            v-for="plateDatas in plateData"
            :key="plateDatas.id"
            :value="plateDatas.id"
            :label="plateDatas.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="汽车车型" prop="plateId">
        <el-select v-model="form.modelId" clearable placeholder="请选择">
          <el-option v-for="modelDatas in modelData" :key="modelDatas.id" :value="modelDatas.id" :label="modelDatas.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="汽修公司" prop="plateId">
        <el-select v-model="form.storeId" clearable placeholder="请选择">
          <el-option v-for="companyDatas in companyData" :key="companyDatas.id" :value="companyDatas.id" :label="companyDatas.companyName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号码" prop="plateNum">
        <el-input v-model="form.plateNum" />
      </el-form-item>
      <el-form-item label="预估到店时间" prop="arriveTime">
        <el-date-picker v-model="form.arriveTime" style="width: 100%" />
      </el-form-item>
      <el-form-item label="维修 | 保养" prop="type">
        <el-select v-model="form.type" clearable placeholder="请选择">
          <el-option value="1" label="维修">维修</el-option>
          <el-option value="2" label="保养">保养</el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="handleConfirm">添加</el-button>
          <el-button @click="handleClose">取消</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch, onMounted } from 'vue'
import { save, modify } from '../../../api/appointment'
import { brandList } from '../../../api/brand'
import { colorList } from '../../../api/color'
import { ElMessage } from 'element-plus'
import { plateList } from '../../../api/Licenseplate'
import { modelList } from '../../../api/model'
import { listAll } from '../../../api/company'

const form = ref({
  name: '',
  phone: '',
  brandId: '',
  colorId: '',
  plateId: '',
  modelId: '',
  storeId: '',
  type: '',
  plateNum: '',
  arriveTime: ''
})

// vue组件加载完成执行
onMounted(() => {
  Query()
})
/**
 * 获取品牌表数据
 * @type {Ref<UnwrapRef<*[]>>}
 */
const brandData = ref([])
const colorData = ref([])
const plateData = ref([])
const modelData = ref([])
const companyData = ref([])
async function Query() {
  // 品牌数据
  const resp = await brandList()
  brandData.value = resp.data.data
  // 颜色数据
  const colorResp = await colorList()
  colorData.value = colorResp.data.data
  // 车牌表数据
  const plateResp = await plateList()
  plateData.value = plateResp.data.data
  // 车型表
  const modelResp = await modelList()
  modelData.value = modelResp.data.data
  const companyResp = await listAll()
  companyData.value = companyResp.data.data
}

const props = defineProps({
  // 标题
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  // 编辑行数据
  dialogRowValue: {
    type: Object,
    default: () => {}
  }
})
// 监听props.dialogRowValue值改变
watch(
  () => props.dialogRowValue,
  () => {
    form.value = props.dialogRowValue
  },
  { deep: true, immediate: true }
)

const isDisabled = ref(false)
onMounted(() => {
  if (props.dialogTitle === '编辑') {
    isDisabled.value = true
  }
})

// 表单校验
const rules = ref({
  name: [
    { required: true, message: '职位不能为空' }
  ]
})

// 修改其他属性的值
const emits = defineEmits(['update:modelValue', 'query'])
// 关闭
function handleClose() {
  emits('update:modelValue', false)
}
// 确定
const formRef = ref(null)
function handleConfirm() {
  formRef.value.validate(async (valid) => {
    if (valid) {
      props.dialogTitle === '添加' ? await save(form.value) : await modify(form.value)
      ElMessage({
        message: '更新成功',
        type: 'success'
      })
      // 上层组件重新获取数据
      emits('query')
      handleClose()
    } else {
      return false
    }
  })
}
</script>

<style class="scss" scoped>
::v-deep .el-input__prefix-inner {
  align-items: center;
}
</style>
